function getProduct(){
	$.ajax({
		url: "getProduct.action",
		type:"post",
		dataType:"json",
		success:function(result){
//			console.log(result)
//			console.log(result)
			ProductTypeTable(result)
		},
		error:function(){
			
		}
	})
}
function ProductTypeTable(data){
	var oTable;
	 oTable = $('#productTypeTable').DataTable({
		destroy: true,
		"bAutoWidth":false,
		columnDefs:[{
			 orderable:false,//禁用排序
			 targets:[4,7,8]   //指定的列
		 }],
		responsive: true,
		data: data,
		//使用对象数组，一定要配置columns，告诉 DataTables 每列对应的属性
		columns: [{
				data: 'product.id'
			},
			{
				data: 'product.producttype.name'
			},
			{
				data: 'product.productName'
			},
			{
				data: 'product.productNo'
			},
			{
				data: 'spec'
			},
			{
				data: 'product.createdTime'
			},
			{
				data: 'product.updateTime'
			},
			{
				data: 'product.remark'
			}
			,
			{
				data: 'product.remark'
			}
		],
		language: {
	        "sProcessing": "处理中...",
	        "sLengthMenu": "显示 _MENU_ 项结果",
	        "sZeroRecords": "没有匹配结果",
	        "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
	        "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
	        "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
	        "sInfoPostFix": "",
	        "sSearch": "搜索:",
	        "sUrl": "",
	        "sEmptyTable": "表中数据为空",
	        "sLoadingRecords": "载入中...",
	        "sInfoThousands": ",",
	        "oPaginate": {
	            "sFirst": "首页",
	            "sPrevious": "上页",
	            "sNext": "下页",
	            "sLast": "末页"
	        },
	        "oAria": {
	            "sSortAscending": ": 以升序排列此列",
	            "sSortDescending": ": 以降序排列此列"
	        }
	    },
		"fnCreatedRow": function( nRow, aData, iDataIndex ){ 
//			$('td:eq(4)',nRow).html("<select class='form-control'><option value='"+aData.spec[0][2]+"'>大杯</option><option value='"+aData.spec[1][2]+"'>中杯</option><option value='"+aData.spec[2][2]+"'>小杯</option></select>")
			var str = [];
			var tmp = []
			for(var i=0;i<aData.spec.length;i++){
//				console.log(aData.spec[i])
//				tmp = "<div class='form-group'><label class='form-label'>"+aData.spec[i][3]+"</label><input type='text' value='"+aData.spec[i][2]+"' class='form-control'></div>"
	    		tmp = "<div class='input-group col-lg-4'><span class='input-group-addon'>"+aData.spec[i][3]+"</span><input type='number' value='"+aData.spec[i][2]+"' class='form-control'></div>"
				str+=tmp
			}
			$('td:eq(4)',nRow).html(str)
			
			$("td:eq(8)",nRow).html("<button class='btn btn-danger' onclick='deleteProduct(this)' >删除</button>")
		    var R2 = $("td:eq(2)",nRow).text()
			$("td:eq(2)",nRow).html("<input class='form-control' value='"+R2+"'>")
			var R7 = $("td:eq(7)",nRow).text()
			$("td:eq(7)",nRow).html("<input class='form-control' value='"+R7+"'>")
			var oldName;
			$("td:eq(7),td:eq(2),td:eq(4)",nRow).children("input").focus(function(){
				oldName = $(this).val()
			})
			$("td:eq(7),td:eq(2),td:eq(4)",nRow).off("change").on("change",function(e){
				var data = {
						"product.productName":$("td:eq(2)",nRow).find("input").val(),
						"product.remark":$("td:eq(7)",nRow).find("input").val(),
						"product.productNo":$("td:eq(3)",nRow).html(),
						"price1":$("td:eq(4)",nRow).find("input").eq(0).val(),
						"price2":$("td:eq(4)",nRow).find("input").eq(1).val(),
						"price3":$("td:eq(4)",nRow).find("input").eq(2).val()
				}
//				console.log(data)
				$.ajax({
					url: "updateProduct.action",
					type:"post",
					dataType:"json",
					data:{
						"product.productName":$("td:eq(2)",nRow).find("input").val(),
						"product.remark":$("td:eq(7)",nRow).find("input").val(),
						"product.productNo":$("td:eq(3)",nRow).html(),
						"price1":$("td:eq(4)",nRow).find("input").eq(0).val(),
						"price2":$("td:eq(4)",nRow).find("input").eq(1).val(),
						"price3":$("td:eq(4)",nRow).find("input").eq(2).val()
					},
					success:function(result){
//						console.log(oldName)
						if(result=="产品名称已存在"){
							alertInfo("alert-danger","失败",result,"ProductTypeTable")
							$(e.target).val(oldName)
						}
						else if(result=="更新成功"){
							alertInfo("alert-success","成功",result,"ProductTypeTable")
						}
					},
					error:function(){
						
					}
				})
			})
		},
		"fnRowCallback": function(nRow, aaData, iDisplayIndex, iDisplayIndexFull) {
//			alert("1'")
		}
	})
}

function findAllStaff(){
	$.ajax({ 
		type: "post",
	       url: "findAllStaff.action",
	       dataType: "json",
	       success: function(result){
	    	suffAccount(result)
		},
		error:function(){
			
		}
	});
}
function suffAccount(data){
//	console.log(data)
	var oTable;
	 oTable = $('#suffAccountTable').DataTable({
		destroy: true,
		"bAutoWidth":false,
		responsive: true,
		data: data,
		//使用对象数组，一定要配置columns，告诉 DataTables 每列对应的属性
		columns: [{
				data: 'staff.id'
			},
			{
				data: 'staff.name'
			},
			{
				data: 'staff.gender'
			},
			{
				data: 'staff.telephone'
			},
			{
				data: 'staff.employedDate'
			},
			{
				data: 'staff.staffNo'
			},
			{
				data: 'staff.password'
			},
			{
				data: 'role'
			}
			,
			{
				data: 'staff.remark'
			},
			{
				data: 'staff.remark'
			}
		],
		language: {
	        "sProcessing": "处理中...",
	        "sLengthMenu": "显示 _MENU_ 项结果",
	        "sZeroRecords": "没有匹配结果",
	        "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
	        "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
	        "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
	        "sInfoPostFix": "",
	        "sSearch": "搜索:",
	        "sUrl": "",
	        "sEmptyTable": "表中数据为空",
	        "sLoadingRecords": "载入中...",
	        "sInfoThousands": ",",
	        "oPaginate": {
	            "sFirst": "首页",
	            "sPrevious": "上页",
	            "sNext": "下页",
	            "sLast": "末页"
	        },
	        "oAria": {
	            "sSortAscending": ": 以升序排列此列",
	            "sSortDescending": ": 以降序排列此列"
	        }
	    },
		"fnCreatedRow": function( nRow, aData, iDataIndex ){ 
//			console.log(aData)
			$('td:eq(1)',nRow).html("<input value='"+$('td:eq(1)',nRow).html()+"'>")
			$('td:eq(3)',nRow).html("<input value='"+$('td:eq(3)',nRow).html()+"'>")
			$('td:eq(6)',nRow).html("<input value='"+$('td:eq(6)',nRow).html()+"'>")
			$('td:eq(8)',nRow).html("<input value='"+$('td:eq(8)',nRow).html()+"'>")
			$('td:eq(1),td:eq(3),td:eq(6),td:eq(8)',nRow).attr("contenteditable","true")
			$('td:eq(1),td:eq(3),td:eq(6),td:eq(8)',nRow).children("input").addClass("focusControl")
			$("td:last",nRow).html("<button onclick='deleteStaff(this)' class='btn btn-default' >删除</button>")
			if($("td:eq(7)",nRow).html()=="staff"){
				$("td:eq(7)",nRow).html("普通员工")
			}
			else{
				$("td:eq(7)",nRow).html("管理员")
			}
			$(nRow).change(function(){
//				console.log(
//			    	"staff.id"+$(this).find("td").eq(0).text()+
//			    	"staff.name"+$(this).find("input").eq(0).val()+
//			    	"staff.telephone"+$(this).find("input").eq(1).val()+
//			    	"staff.password"+$(this).find("input").eq(2).val()+
//			    	"staff.remark"+$(this).find("input").eq(3).val()
//			    )
				$.ajax({ 
					type: "post",
				    url: "updateStaff.action",
				    dataType: "json",
				    data:{
				    	"staff.id":$(this).find("td").eq(0).text(),
				    	"staff.name":$(this).find("input").eq(0).val(),
				    	"staff.telephone":$(this).find("input").eq(1).val(),
				    	"staff.password":$(this).find("input").eq(2).val(),
				    	"staff.remark":$(this).find("input").eq(3).val()
				    },
				    success: function(result){
				    	alertInfo("alert-success","成功！","修改成功!!","suffAccount")
					},
					error:function(){
						
					}
				})
			})
		},
		"fnRowCallback": function(nRow, aaData, iDisplayIndex, iDisplayIndexFull) {
		}
	})
}
function deleteStaff(a){
	$.ajax({ 
		type: "post",
	    url: "deleteStaff.action",
	    dataType: "json",
	    data:{
	    	"staff.staffNo":$(a).parents("tr").find("td").eq(5).text()
	    },
	    success: function(result){
//	    	console.log(result)
	    	$(a).parents("tr").remove()
		},
		error:function(){
			
		}
	})
}